//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "player\fn_safeEject.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_unit","_captive_fall","_vehicle","_in_chute"];
DEFAULT_PARAM(_unit,0,objNull);
DEFAULT_PARAM(_captive_fall,1,false);
if (!isNull _unit && {vehicle _unit != _unit}) then {
	_unit allowDamage false;
	sleep (0.5 + random(1));
	if ((vehicle _unit != _unit) && !((vehicle _unit) isKindOf "ParachuteBase")) then {
		_vehicle = (vehicle _unit);
		_unit action["eject",_vehicle];
		waituntil { vehicle _unit != _vehicle };
		_unit setPos (_vehicle modelToWorld [random(5),random(5),-10]);
	};
	if (_captive_fall) then {
		_unit setCaptive true;
		_unit spawn {
			waitUntil { ((getPos _this) select 2) <= 1 };
			_this setCaptive false;
		};
	};
	_unit spawn {
		sleep 10;
		_this allowDamage true;
	};
};
TRACE("End");
TMFPROFILERSTOP;